package com.kj.tms.system.controller.menu;


import com.kj.tms.common.bean.dto.Pager;
import com.kj.tms.common.bean.dto.ReturnData;
import com.kj.tms.common.utils.constant.Consts;
import com.kj.tms.pri.system.menu.dto.LoggerEntity;
import com.kj.tms.pri.system.menu.service.LoggerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("rawtypes")
@Controller
@RequestMapping("/system/log")
public class LogController {

    @Autowired
    private LoggerService logService;

    /**
     * 日志首页
     *
     * @return
     * @throws
     */
    @RequestMapping("/index")
    public String index() {
        return "system/log/index";
    }

    /**
     * 分页查询数据
     *
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/queryByPage")
    @ResponseBody
    public ReturnData queryByPage(LoggerEntity model) {
        Pager pager = null;
        try {
            pager = logService.listByCondition(model);
        } catch (Exception e) {
            return new ReturnData(Consts.ERROR, e.getMessage(), e.getMessage());
        }
        //设置页面数据
        Map<String, Object> returnObject = new HashMap<String, Object>();
        returnObject.put("total", pager.getRowCount());
        returnObject.put("rows", pager.getResults());
        return new ReturnData(Consts.SUCCESS, "", returnObject);
    }

    /**
     * @Method toView
     * @Param 主键
     * @Return
     * @Exception
     * @Description [方法描述]
     * @Author yangruidong
     * @Version 1.0
     * @Date 2018/7/10 9:54
     */
    @RequestMapping("/toView")
    public String toView(String id, Model model) {
        model.addAttribute("id", id);
        return "system/log/view";
    }

    /**
     * @Method view
     * @Param
     * @Return
     * @Exception
     * @Description [详细信息展示]
     * @Author yangruidong
     * @Version 1.0
     * @Date 2018/7/10 9:57
     */
    @RequestMapping("/view")
    @ResponseBody
    public ReturnData view(LoggerEntity model) {
        Pager pager = null;
        try {
            pager = logService.listByCondition(model);
        } catch (Exception e) {
            return new ReturnData(Consts.ERROR, e.getMessage(), e.getMessage());
        }
        //设置页面数据
        Map<String, Object> returnObject = new HashMap<String, Object>();
        returnObject.put("rows", pager.getResults());
        return new ReturnData(Consts.SUCCESS, "", returnObject);
    }

    /**
     * @method      toElkView
     * @description [查询elk配置的服务器路径]
     * @author      [huangdongdong]
     * @version     1.0
     * @param       model
     * @return      java.lang.String
     * @exception   []
     * @date        2018/7/24 15:37
     */
    @RequestMapping("/toElkView")
    public String toElkView( Model model) {
        String curl = logService.getElkUrl("elkUrl");
        model.addAttribute("curl", curl);
        return "system/log/elk";
    }
}
